<?php

namespace App\Repository\XhApi;

use App\Models\Xh\QuestionModel;
use App\Repository\BaseRepository;

class QuestionRepository extends BaseRepository
{
    public $model;
    public $log;

    public function __construct()
    {
        $this->model = new QuestionModel();
        parent::__construct();
    }

    public function index($params, $page = 1, $limit = 10)
    {
        $where = [];
        if (!empty($params['shop_id'])) {
            $where['where']['shop_id'] = $params['shop_id'];
        }
        if (isset($params['type']) && $params['type'] != '') {
            $where['where']['type'] = (int)$params['type'];
        }
        if (!empty($params['dateRange'])) {
            $time = $params['dateRange'];
            $where['between']['created_at'] = $time;
        }
//        拉修改资料
        if (isset($params['id']) && $params['id'] > 0) {
            $row = $this->unifiedGetOne(['id' => $params['id']]);
            if (!empty($row)) {
                $row['answer'] = rawurlencode(html_entity_decode($row['answer']));
            }
            return $this->returnMsg(200, '查询成功', $row, 1);
        }
        $field = $this->model->getTableField();
        $field = array_values(array_diff($field, ['answer']));
        return $this->unifiedGetPage($where, $page, $limit, $field);
    }

//删除指定id的内容
    public function del($id, $shopId = 0)
    {
        return $this->unifiedDel($id, $shopId);
    }

//新增内容
    public function add($postData)
    {
        $shop_id = $postData['user']['shop_id'] ?? 0;
        return $this->unifiedAdd($postData, [
            'title' => '标题必填写',
            'answer' => '请填写回答内容'
        ], null, $shop_id);
    }

//修改内容
    public function edit($postData)
    {
        $shop_id = $postData['user']['shop_id'] ?? 0;
        return $this->unifiedEdit($postData, [
            'id' => '请指明修改的数据编号',
            'title' => '标题必填写',
            'answer' => '请填写回答内容'
        ], null, $shop_id);
    }
}
